package com.ad.pet.message;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.CommList;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.message.Message;

import java.util.List;

import javax.inject.Inject;


/**
 * Created by SEELE on 2018/8/20.
 */
@PerActivity
public class MessagePresenter extends AbsBasePresenter implements MessageContract.Presenter{
    private ApiService mApiService;
    private MessageContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public MessagePresenter(ApiService apiService,
                               Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }
    @Override
    public void onCreate(MessageContract.View view) {
        this.mView = view;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable!=null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }

    @Override
    public void getMessage() {
        mRxDisposable.add(mApiService.message_category_list()
                .subscribe(new AppSubscriber<List<Message>>(mView) {
                    @Override
                    public void next(List<Message> list) {
                        mView.getMessageSuccess(list);
                    }
                }));
    }

    @Override
    public void getTypeMessage(String cid) {
        mRxDisposable.add(mApiService.msg_list(cid)
                .subscribe(new AppSubscriber<CommList<List<Message>>>(mView) {
                    @Override
                    public void next(CommList<List<Message>> list) {
                        mView.getTypeMessageSuccess(list.getList());
                    }
                }));
    }

    @Override
    public void isReadMessage(String id) {
        mRxDisposable.add(mApiService.messageRead(id)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User list) {
                        mView.isReadMessageSuccess();
                    }
                }));
    }

    @Override
    public void isTypeReadMessage(String cid) {
        mRxDisposable.add(mApiService.messageTypeRead(cid)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User list) {
                        mView.isTypeReadMessageSuccess();
                    }
                }));
    }

    @Override
    public void deleteMessage(String id) {
        mRxDisposable.add(mApiService.message_delete_id(id)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User list) {
                        mView.deleteMessageSuccess();
                    }
                }));
    }


}
